package com.learn.order.schedule;

import com.learn.order.domain.Order;
import com.learn.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时任务 配置类
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2025/2/23 20:28
 * @updateUser:     MI
 * @updateTime:     2025/2/23 20:28
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
@Component
public class OrderCancelSchedule {

    @Autowired
    private OrderService orderService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void cancelUnpaidOrders() {
        List<Order> unpaidOrders = orderService.getUnpaidOrders();
        unpaidOrders.forEach(order -> {
            if (order.getCreationTime().plusMinutes(30).isBefore(LocalDateTime.now())) {
                orderService.cancelOrder(order.getId());
            }
        });
    }
}